package controller.command;

import model.spring.AbstractAddress;
import model.spring.AddressList;

public class Save extends Command {

	private AbstractAddress address,bkp;
	private int position;
	
	public Save(AbstractAddress address, AbstractAddress bkp, int position){
		this.address = address;
		this.bkp = bkp;
		this.position = position;
	}
	
	@Override
	public void execute() {
		if(bkp != null){
			AddressList.getInstance().remove(position);
			AddressList.getInstance().add(address,position);
		} else {
			position = AddressList.getInstance().size();
			AddressList.getInstance().add(address,position);
		}
		System.out.println(this.getClass()+" exec -> position: "+position+" flag: "+AddressList.getInstance().get(position).isDirty());
	}

	@Override
	public void undo() {
		if(bkp != null){
			AddressList.getInstance().remove(position);
			AddressList.getInstance().add(bkp,position);
		} else {
			AddressList.getInstance().remove(position);
		}
	}

}
